package com.gitee.elf.core.data

import com.google.gson.*
import org.bukkit.Bukkit
import org.bukkit.Location
import taboolib.common5.cdouble
import taboolib.common5.cfloat
import java.lang.reflect.Type

class DataLocation(val value: Location) : Data {

    override fun getValue(): Any {
        return value
    }

    override fun clone(): Data {
        return DataLocation(value.clone())
    }

    @SerializerType(Location::class)
    class Serializer : JsonSerializer<DataLocation>, JsonDeserializer<DataLocation> {

        override fun serialize(src: DataLocation?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
            val value = src!!.value
            return JsonPrimitive("${value.world!!.name} ${value.x} ${value.y} ${value.z} ${value.yaw} ${value.pitch}")
        }

        override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): DataLocation {
            val split = json!!.asString.split(" ")
            return DataLocation(Location(Bukkit.getWorld(split[0]), split[1].cdouble, split[2].cdouble, split[3].cdouble, split[4].cfloat, split[5].cfloat))
        }

    }

}